﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.View;
using System.IO;
using System.Threading;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
            
            //registering for events
            //FolderListViewModel folderModel = (FolderListViewModel)App.FolderView.DataContext;
            ((MainWindowViewModel)this.DataContext).FolderAdded += new MainWindowViewModel.FolderAddedDelegate(folderModel_FolderAdded);
        }

        void folderModel_FolderAdded(string folderName)
        {
            //the user wants to display a new folder
            mainFE.AddFolderToView(folderName);
        }

        public void InitProgressBar(int max, string action)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {

                switch (App.CurrentAppStatus)
                {
                    case App.AppStatus.ReadingFiles: { progressBarTaskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal; App.Logger.FilesProcessed = 0; break; }
                    case App.AppStatus.SearchingCovers: { progressBarTaskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Paused; App.Logger.CoversFound = 0; break; }
                    case App.AppStatus.ApplyingCovers: { progressBarTaskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error; App.Logger.FilesSaved = 0; break; }
                    default: { progressBarTaskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None; break; }
                }

                App.MainProgressBarView = new ProgressBarView();
                App.MainProgressBarView.ShowProgressBar(max,App.MainView);

            })
           );
        }

        public void IncrementTaskBar()
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {  
                double singleIncrement = (100 / App.MainProgressBarView.Maximum) * 0.01;
                progressBarTaskBar.ProgressValue += singleIncrement; // X:1=100:MAX --> percentage of increment
                if (App.MainProgressBarView.Value >= App.MainProgressBarView.Maximum)
                    progressBarTaskBar.ProgressValue = 0;   
            })
           );
        }

        //public void StopProgressBar()
        //{
        //    this.Dispatcher.BeginInvoke((Action)(() =>
        //    {
        //        statusPB.ToolTip = String.Empty;
        //        progressBarTaskBar.ProgressValue = 0;
        //        statusPB.IsIndeterminate = false;
        //        statusPB.Value = statusPB.Minimum;
        //        countTB.Visibility = System.Windows.Visibility.Collapsed;
        //    })
        //    );
        //}

        public string SelectedImagePath { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        private void mainFE_SelectedFolderChanged(string folderName)
        {
            this.Dispatcher.Invoke((Action)(() =>
           {
               //the user has selected a new folder, we need to display all the files inside
               FolderListView folderView = App.FolderView;
               FolderListViewModel modelView = (FolderListViewModel)folderView.DataContext;
               modelView.LoadMp3FromFolder(folderName);
               mp3FilesTB.Text = ""+modelView.CurrentItems.Count;
           }));
        }
       
     
    }
}
